/*
 * Copyright (c) 2020, chao
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2020-08-21     chao         the first version
 */

#include <rtthread.h>
#include "emxgui.h"
#include "x_libc.h"
#include "gui_demo.h"

#define BUTTON_COUNT 7

#define ID_BTN1 0x1101
#define ID_BTN2 0x1102
#define ID_BTN3 0x1103
#define ID_BTN4 0x1104
#define ID_BTN5 0x1105
#define ID_BTN6 0x1106
#define ID_BTN7 0x1107
#define ID_BTN8 0x1108
#define ID_BTN9 0x1109

LRESULT win_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    RECT rc;
    GetClientRect(hwnd, &rc);

    switch (msg)
    {
    case WM_CREATE:
    {
        int lineButtonCount = 3;
		int columnButtonCount = BUTTON_COUNT % lineButtonCount ? BUTTON_COUNT / lineButtonCount + 1 : BUTTON_COUNT / lineButtonCount;
		int buttonWidth = 60;
		int buttonHeight = 80;
		int buttonXDistance = (rc.w - lineButtonCount * buttonWidth) / (lineButtonCount + 1);
		int buttonYDistance = (rc.h - columnButtonCount * buttonHeight) / (columnButtonCount + 1);
		int i;
		int x, y;

		for (i = 1; i <= BUTTON_COUNT; i++)
		{
			int resX = (i % lineButtonCount) ? (i % lineButtonCount) : lineButtonCount;
			int resY = (i - 1 + lineButtonCount) / lineButtonCount;
            WCHAR buff[32] = {0};
			
			x = resX * buttonXDistance + (resX - 1)*buttonWidth;
			y = resY * buttonYDistance + (resY - 1)*buttonHeight;

            x_wsprintf(buff, _T("%d"), i);
			CreateWindow(BUTTON, buff, WS_VISIBLE, x, y, buttonWidth, buttonHeight, hwnd, ID_BTN1+i-1, NULL, NULL);
		}  
        return TRUE;
    }    

    case WM_NOTIFY: //WM_NOTIFY消息:wParam低16位为发送该消息的控件ID,高16位为通知码;lParam指向了一个NMHDR结构体.
    {
        u16 code, id;
        //          NMHDR *nr=(NMHDR*)lParam;
        id = LOWORD(wParam);
        code = HIWORD(wParam);
        if (id >= ID_BTN1 && id <= ID_BTN9)
        {
            if (code == BN_PUSHED)
            { //按钮被按下了.
                GUI_Printf("Button PUSHED: ID:%04XH\r\n", id);
            }

            if (code == BN_CLICKED)
            { //按钮弹起了.
                GUI_Printf("Button UNPUSHED: ID:%04XH\r\n", id);
                if (id == ID_BTN7)
                {
                    GUI_Thread_Create((void(*)(void *))gui_demo_setting, "Setting", 2048, NULL, 5, 5);
                }
            }
        }
    }
    break;

    case WM_PAINT:
    {
    #if 0
        PAINTSTRUCT ps;

        hdc = BeginPaint(hwnd, &ps);
        SetTextColor(hdc, MapRGB(hdc, 0, 0, 255));
        TextOut(hdc, 8, 4, L"Button Test:", -1);
        EndPaint(hwnd, &ps);
    #endif
        break;
    }    

	case WM_ERASEBKGND:
	{
        HDC hdc = (HDC)wParam;
		SetBrushColor(hdc, BK_COLOR(hdc));
		FillRect(hdc, &rc);
		GUI_Printf("MAIN WM_ERASEBKGND\n");
		return TRUE;
	}

    default:
        return DefDesktopProc(hwnd, msg, wParam, lParam);
    }

    return WM_NULL;
}

void GUI_DEMO_Main(void *param)
{
    HWND hwnd;
    WNDCLASS wcex;
    MSG msg;

    wcex.Tag = WNDCLASS_TAG;
    wcex.Style = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc = win_proc;
    wcex.cbClsExtra = 0;
    wcex.cbWndExtra = 0;
    wcex.hInstance = NULL;
    wcex.hIcon = NULL;
    wcex.hCursor = NULL;

    hwnd = CreateWindowEx(WS_EX_LOCKPOS, &wcex, L"Main", 
    WS_VISIBLE|WS_CLIPCHILDREN, 
    0, STATUS_BAR_HEIGHT, GUI_XSIZE, GUI_YSIZE-STATUS_BAR_HEIGHT, NULL, 0, NULL, NULL);

    ShowWindow(hwnd, SW_SHOW);

    while (GetMessage(&msg, hwnd))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
}

